var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    deliveryGoodsList: {
      type: Array,
      value: [],
      observer(newVal, oldVal, changedPath) {
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    supplyChannel:1,//默认一件代发
  },
  /**
   * 组件的方法列表
   */
  methods: {


    // 加入购物车后修改数量
    updateCartNum(e) {
      let total = e.detail.total;
      let index = e.detail.index;
      this.setData({
        ["deliveryGoodsList[" + index + "].cartNum"]: this.data.deliveryGoodsList[index].cartNum > 0 ? this.data.deliveryGoodsList[index].cartNum + total : total
      })
    },


    // 有规格的商品调用规格
    onaddCart(e) {
      let goods = e.detail.goods;
      let index = e.detail.index;
      let platform = e.detail.platform;
      if (platform == 'self') {
        this.selectComponent("#suk").getProductsDetail(goods.id, index,this.data.supplyChannel);
      }
    },
    ajaxAddCart(index, goods, plus = true) {
      let that = this;
      let userinfo = app.functions.getLocalStorage("user");
      let shop = app.functions.getLocalStorage("shop");
      if (app.functions.isEmpty(userinfo)) {
        app.functions.islogin();
        return false;
      }
      let cart = {
        supplyChannel:1,
        mid: userinfo.id,
        teamid: shop.id,
        productId: goods.id,
        total: plus ? 1 : -1,
        marketprice: goods.marketprice
      };
      //修改吉贡单规格商品
      if (goods.skus.length == 1) {
        let skusOne = goods.skus[0];
        cart.skuId = skusOne.id;
        cart.skuTitle = skusOne.title;
        cart.marketprice = (user.isVip && skusOne.vipprice && goods.isvip) ? skusOne.vipprice : skusOne.marketprice;
        cart.vipprice = skusOne.vipprice;
      }

      if (goods.maxbuy && (goods.cartNum + cart.total) > goods.maxbuy) {
        app.functions.showToast("已经超过单次购买数量");
        return;
      }
      app.api.cartAdd(cart, (response) => {
        if (response.code != 0) {
          app.functions.showToast("加入到购物车失败");
          return;
        }
        app.functions.showToast("已加入到购物车");
        app.cartNumber = app.cartNumber == 0 ? cart.total : parseInt(app.cartNumber) + cart.total;
        that.setData({
          ["items.cartNum"]: this.data.items.cartNum + cart.total
        })
        app.UpdateCart();
      }, (error) => {

      })
    },

  }
})
